//
//  YeeTitleScrollView.m
//  HappyRent
//
//  Created by CoderYee on 2017/3/30.
//  Copyright © 2017年 内圈科技. All rights reserved.
//

#import "YeeTitleScrollView.h"

// 默认标题间距
static CGFloat const margin = 20;

@interface YeeTitleScrollView ()

@property (nonatomic, strong) UIColor *titleScrollViewColor;

@property (nonatomic, weak) UIScrollView *titleScrollView;

@property (nonatomic, strong) NSMutableArray *titleLabels;

/** 所以标题宽度数组 */
@property (nonatomic, strong) NSMutableArray *titleWidths;

@property (nonatomic, weak)  UIView *underLine;

@property (nonatomic, strong) UIFont *titleFont;

/** 标题间距 */
@property (nonatomic, assign) CGFloat titleMargin;


@end

@implementation YeeTitleScrollView

-(instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray{
    if (self=[super initWithFrame:frame]) {
//        _allowEqualWidth=YES;
        self.titleScrollView.frame=self.bounds;
        [self.underLine setBackgroundColor:[UIColor redColor]];
        [self setUpAllTitle:titleArray];
    }
    return self;
}
// 设置所有标题
- (void)setUpAllTitle:(NSArray *)titles
{
    for (NSInteger  i=0; i<titles.count; i++) {
        
        CGFloat itemW = [self calculateSizeWithFont:14 Text:titles[i]].size.width;
        if (_allowEqualWidth) {
            
            itemW =kMainScreenWidth/4.0;
        }
        UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(self.titleMargin+margin, 0, itemW, self.frame.size.height)];
        lable.tag = i;
        lable.textAlignment=NSTextAlignmentCenter;
        lable.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleClick:)];
        [lable addGestureRecognizer:tap];
        lable.font=self.titleFont;
        lable.text=titles[i];
        [self.titleScrollView addSubview:lable];
        [self.titleLabels addObject:lable];
        [self.titleWidths addObject:@(itemW)];
        self.titleMargin += itemW+margin;
        
        if (i == _selectIndex) {
            [self titleClick:tap];
        }
    }
    self.titleScrollView.contentSize = CGSizeMake(self.titleMargin, self.frame.size.height);
}
#pragma mark  标题按钮点击
- (void)titleClick:(UITapGestureRecognizer *)tap
{
    // 获取对应标题label
    UILabel *label = (UILabel *)tap.view;
    // 获取当前角标
    NSInteger i = label.tag;
    // 选中label
    [self selectLabel:label];
    
    _selectIndex = i;
    if (self.delegte&&[self.self.delegte respondsToSelector:@selector(didselectedindex:)]) {
        
        [self.delegte didselectedindex:_selectIndex];
    }
}
-(void)scrolltoIndex:(NSInteger )Index{
    
    UILabel *labe=self.titleLabels[Index];
    [self selectLabel:labe];
}
- (void)selectLabel:(UILabel *)label
{
    for (UILabel *labelView in self.titleLabels) {
        
        if (label == labelView) continue;
        labelView.transform = CGAffineTransformIdentity;
        labelView.textColor = [UIColor blackColor];;
     }
    // 修改标题选中颜色
    label.textColor = [UIColor redColor];
    // 设置标题居中
    [self setLabelTitleCenter:label];
    [self setUpUnderLine:label];

}
// 设置下标的位置
- (void)setUpUnderLine:(UILabel *)label
{

    CGFloat underLineH = 1.5;
    self.underLine.mj_y = label.height - underLineH;
    self.underLine.height = underLineH;
    // 点击时候需要动画
    [UIView animateWithDuration:0.25 animations:^{
        self.underLine.width = label.width;
        self.underLine.centerX = label.centerX;
    }];
    
}
// 让选中的按钮居中显示
- (void)setLabelTitleCenter:(UILabel *)label
{
    // 设置标题滚动区域的偏移量
    CGFloat offsetX = label.center.x - kMainScreenWidth * 0.5;
    
    if (offsetX < 0) {
        offsetX = 0;
    }
    
    // 计算下最大的标题视图滚动区域
    CGFloat maxOffsetX = self.titleScrollView.contentSize.width - kMainScreenWidth + _titleMargin;
    
    if (maxOffsetX < 0) {
        maxOffsetX = 0;
    }
    
    if (offsetX > maxOffsetX)
    {
        offsetX = maxOffsetX;
    }
    // 滚动区域
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

- (UIFont *)titleFont
{
    if (_titleFont == nil) {
        _titleFont = [UIFont systemFontOfSize:14];
    }
    return _titleFont;
}

- (UIView *)underLine
{
    if (_underLine == nil) {
        
        UIView *underLineView = [[UIView alloc] init];
        
        underLineView.backgroundColor = [UIColor redColor];
        
        [self.titleScrollView addSubview:underLineView];
        
        _underLine = underLineView;
        
    }
    return _underLine ;
}
// 懒加载标题滚动视图
- (UIScrollView *)titleScrollView
{
    if (_titleScrollView == nil) {
        
        UIScrollView *titleScrollView = [[UIScrollView alloc] init];
        titleScrollView.scrollsToTop = NO;
        titleScrollView.backgroundColor = _titleScrollViewColor?_titleScrollViewColor:[UIColor colorWithWhite:1 alpha:0.7];
        
        [self  addSubview:titleScrollView];
        
        _titleScrollView = titleScrollView;
        
    }
    return _titleScrollView;
}

- (NSMutableArray *)titleLabels
{
    if (_titleLabels == nil) {
        
        _titleLabels = [NSMutableArray array];
    }
    return _titleLabels;
}
- (NSMutableArray *)titleWidths
{
    if (_titleWidths == nil) {
        _titleWidths = [NSMutableArray array];
    }
    return _titleWidths;
}
-(CGRect)calculateSizeWithFont:(NSInteger)Font Text:(NSString *)Text{
    NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:Font]};
    CGRect size = [Text boundingRectWithSize:CGSizeMake(MAXFLOAT, self.frame.size.height)
                                     options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attr
                                     context:nil];
    return size;
}
@end
